﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

namespace MCPro
{
    public static class Extensions
    {
        /// <summary>
        /// Validation for string if valid Integer (Int32)
        /// </summary>
        /// <param name="value">String to validate</param>
        /// <returns></returns>
        public static bool IsNumber(this string value)
        {
            try
            {
                Int32.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Validation for an object if valid Integer (Int32)
        /// </summary>
        /// <param name="value">Object to validate</param>
        /// <returns></returns>
        public static bool IsNumber(this object value)
        {
            try
            {
                Int32.Parse(value.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Validation for an object if valid Long Integer (Int64)
        /// </summary>
        /// <param name="value">String to validate</param>
        /// <returns></returns>
        public static bool IsIntLong(this string value)
        {
            try
            {
                Int64.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Validation for an object if valid Decimal
        /// </summary>
        /// <param name="value">String to validate</param>
        /// <returns></returns>
        public static bool IsDecimal(this string value)
        {
            try
            {
                Decimal.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Validation for a string if valid DateTime
        /// </summary>
        /// <param name="value">String to validate</param>
        /// <returns></returns>
        public static bool IsDate(this string value)
        {
            try
            {
                DateTime.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        ///  Validation for a string if valid DateTime
        /// </summary>
        /// <param name="value">String to validate</param>
        /// <param name="_value">reference value of the validated string</param>
        /// <returns></returns>
        public static bool IsDate(this string value, ref DateTime _value)
        {
            DateTime retVal = new DateTime();
            try
            {

                retVal = DateTime.Parse(value);
                _value = retVal;
                return true;
            }
            catch (Exception ex)
            {
                _value = DateTime.Parse("1/1/1900");
                return false;
            }
        }

        /// <summary>
        /// Validation for a string if valid Email
        /// </summary>
        /// <param name="value">String to validate</param>
        /// <returns></returns>
        public static bool IsEmail(this string value)
        {
            try
            {
                System.Net.Mail.MailAddress _value = new System.Net.Mail.MailAddress(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Convert string to integer
        /// </summary>
        /// <param name="value">string value</param>
        /// <returns></returns>
        public static int ToInt(this string value)
        {
            try
            {
                int i = Convert.ToInt32(value);
                return i;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// Convert Generic List to DataTable
        /// </summary>
        /// <typeparam name="?">string</typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

        public static DataTable FromJsonToDataTable(this string JsonData)
        {
            DataTable dt = (DataTable)JsonConvert.DeserializeObject(JsonData, (typeof(DataTable)));
            return dt;
        }
    }
}